package com.umakr.ax.app.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.mapper.AppVersionMapper;
import com.umakr.ax.common.model.AppVersion;
import com.umakr.ax.common.model.PluginCouponVerPeple;
import com.umakr.ax.common.model.RadioStation;
import com.umakr.ax.common.model.SeekHelpInfo;
import com.umakr.ax.common.service.RadioStationService;
import com.umakr.ax.common.vo.RadioStationShowVO;
import com.umakr.ax.common.vo.RadioStationVO;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.opt.service.UserOptService;
import com.umakr.ax.utils.*;
import net.dongliu.apk.parser.ApkParser;
import net.dongliu.apk.parser.bean.ApkMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;


/**
 * <p>
 * 用户详情 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class AppUploadService extends BaseService<AppVersionMapper, AppVersion>{
    @Autowired
    RadioStationService radioStationService;
    @Autowired
    AppUploadService appUploadService;
    @Autowired
    UserOptService userOptService;
    @Value("${oss.img.path}")
    public String aliUrl;
    @Value("${local.file.upload.apk}")
    public String nginxBaseUploadDir;


    public Object fileUpload(MultipartFile file) throws IOException {
        Map<String,Object> result = Maps.newHashMap();
        String filePath = FileUploadUtil.fileUpload(file,nginxBaseUploadDir);
        result.put("filePath", filePath.replace(nginxBaseUploadDir,""));
        if(filePath.startsWith(nginxBaseUploadDir)){
            ApkParser apkParser = new ApkParser(new File(filePath));
            ApkMeta apkMeta = apkParser.getApkMeta();
            result.put("apkMeta",apkMeta);
            apkParser.close();
            return resultMap(result,1,"上传成功");
        }else{
            return resultMap(0,filePath);
        }
    }


    public Object listHisVersion(Integer versionCode,Integer versionType) {
        Page<AppVersion> appVersionPage = baseMapper.listHisVersion(versionCode,versionType);
        return appVersionPage;
    }


    public Object saveNewVersion(AppVersion appVersion) {
        int errorCode = 0 ;
        String msg = "";
        appVersion.setStime(new Date());
        baseMapper.saveNewVersion(appVersion);
        return resultMap(appVersion,1,"创建成功");
    }

    public Object delectApk(String apkUrl) {
        int errorCode = 0 ;
        String msg = "移除失败";
        File f = new File(nginxBaseUploadDir+apkUrl);
        if(f.exists() && f.isFile()){
            if(f.delete()) {
                errorCode = 1;
                msg = "移除成功";
            }
        }
        return resultMap(errorCode,msg);

    }
}
